<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Xcx\Controller;

use Xcx\Model\QuestionModel;
use Xcx\Model\AccountantModel;
use Xcx\Model\UsersModel;
use Xcx\Model\UserModel;

class UserController extends XcxBaseController {

    /**
     * 我的提问列表
     */
    public function question_list() {
        if(!$this->user_id){
            jsonReturn(0);
        }
        $questionModel = new QuestionModel();
        $post = I('post.');
        $where = 'q.user_id = '.$this->user_id;
        $orderby = 'q.add_time desc';
        $count = $questionModel->getList($where,1,$post['limit'],true,$orderby);
        $list = $questionModel->getList($where,$post['page'],$post['limit'],false,$orderby);

        $return['data'] = $list;
        $return['page'] = $post['page'];
        $return['total'] = $count;
        jsonReturn($return);
    }

    /**
     * 我的收藏列表
     */
    public function collect_list() {
        if(!$this->user_id){
            jsonReturn(0);
        }
        $questionModel = new QuestionModel();
        $post = I('post.');
        $count = $questionModel->getCollectByUser($this->user_id,1,$post['limit'],true);
        $list = $questionModel->getCollectByUser($this->user_id,$post['page'],$post['limit']);

        $return['data'] = $list;
        $return['page'] = $post['page'];
        $return['total'] = $count;
        jsonReturn($return);

    }

    /**
     * 我的账号信息
     */
    public function get_info() {
        if(!$this->user_id){
            jsonReturn();
        }
        $userModel = new UsersModel();
        $list = $userModel->getInfoById($this->user_id);
        if($list){
            $list['has_response'] = getQuestionResponse($this->user_id);
        }
        jsonReturn(array('data' => $list));
    }

    /**
     * 获取地区列表
     */
    public function area_list() {
        $userModel = new UsersModel();
        $lists = $userModel->getAreaList();
        jsonReturn(array('data' => $lists));
    }

    /**
     * 修改地区
     */
    public function update_area() {
        if(!$this->user_id){
            jsonReturn();
        }
        $post = I('post.');
        if($post['province_name'] && $post['city_name'] ){
            $city = M('region')->where("name like '".$post['city_name']."%' and level = 2")->find();
            if($city){
                $post['city_id'] = $city['id'];
                $province = M('region')->where(" id = $city[parent_id] and level = 1")->find();
                $post['province_id'] = $province['id'] ? $province['id']  : 0;
            }
            if(!$post['province_id']){
                $province = M('region')->where("name like '".$post['province_name']."%' and level = 1")->find();
                $post['province_id'] = $province['id'] ? $province['id']  : 0;
            }
        }elseif(!$post['province_id'] || !$post['city_id'] ){
            jsonReturn();
        }
        $data = array(
            'province' => $post['province_id'] ? $post['province_id'] : 0,
            'city' => $post['city_id'] ? $post['city_id'] : 0,
        );
        if(!$data['province']){
            jsonReturn( false, '获取不到对应的省份');
        }
        if(!$data['city']){
            jsonReturn( false, '获取不到对应的城市');
        }
        $userModel = new UsersModel();
        $res = $userModel->updateInfo($this->user_id,$data);
        if($res['status']) {
            jsonReturn(array('province' => $data['province'] , 'city' => $data['city']));
        }
        else
            jsonReturn( $res['status'] , $res['msg']);
    }

    /**
     * 获取验证码
     */
    public function get_code() {
        if(!$this->user_id){
            jsonReturn();
        }
        $phone_num = I('post.phone_num');

        $userModel = new UserModel();
        $res = $userModel->sendSmsCode($phone_num);
        $bool = $res['status'] == 1 ? true : false;
        jsonReturn( $bool , $res['msg']);
    }

    /**
     * 更新手机号
     */
    public function update_mobile() {
        if(!$this->user_id){
            jsonReturn();
        }
        $phone_num = I('post.phone_num');
        $code = I('post.code');

        $userModel = new UserModel();
        $res = $userModel->checkSmsCode($phone_num,$code);
        $bool = $res['status'] == 1 ? true : false;
        $msg = $res['msg'];
        if($bool){
            $userModel = new UsersModel();
            $res = $userModel->updateInfo($this->user_id,array('mobile' => $phone_num, 'mobile_validated' => 1));
            $bool = $res['status'] == 1 ? true : false;
            $msg = $res['status'] == 1 ? '更新成功' : $res['msg'];
        }
        jsonReturn( $bool , $msg);
    }



}
